(function() {
    Nokia.IconicMenu = Nokia.Widget.extend({
        initialize: function(options) {
            var instance = this;

			var defaults = {
				maximize: false,
				keepState: true
			};

            instance.options = jQuery.extend(defaults, options);

            instance.element = jQuery( instance.options.element );

            instance._super.apply(instance, [instance.options]);

            instance.registerData("iconicmenu");

            instance._create();

			instance.registerVibrationOn([]);
			instance.fireCallback('create');
        },

		_create: function() {
			var instance = this,
				element = instance.element,
				options = instance.options,
				Styles = instance.Styles,
				items = options.items;

			instance.setItems(items);

			if (options.maximize) {
				Nokia.util.maximize(element);
			}

			Nokia.util.addClass( element[0], [Styles.uiClearfix, Styles.uiStateDefault, Styles.iconicMenu].join(' ') );

			instance.wrapper = Nokia.dom.parseHTML('<div class="' + [Styles.iconicMenuItemsWrapper, Styles.uiClearfix].join(' ') + '"></div>');

			Nokia.dom.append(element, instance.wrapper);

			jQuery.each(instance.items, function(i, item) {
				var itemContainer = Nokia.dom.parseHTML('<a href="javascript:void(0);" class="' + [Styles.iconicMenuItem].join(' ') + '"></a>');
				var image = Nokia.dom.parseHTML('<img src="' + instance.items[i].icon +'" class="' + [Styles.iconicMenuImage].join(' ') + '" />');
				var label = Nokia.dom.parseHTML('<p class="' + [Styles.iconicMenuLabel].join(' ') + '">' + instance.items[i].label +'</p>');

				Nokia.dom.append(itemContainer, image);
				Nokia.dom.append(itemContainer, label);

				var itemAction = function(event) {
					instance.select(instance.items[i], event);

					if (options.keepState && instance.items[i].activeIcon) {
						Nokia.util.addClass( itemContainer[0], Styles.uiStateActive );

						image.attr('src', instance.items[i].activeIcon);

						if (instance.currentItemImage && !instance.equalsItems(instance.currentItem, instance.items[i])) {
							Nokia.util.removeClass( instance.currentItemContainer[0], Styles.uiStateActive );

							instance.currentItemImage.attr('src', instance.currentItem.icon);
						}

					} else {
						Nokia.util.removeClass( itemContainer[0], Styles.uiStateActive );

						image.attr('src', instance.items[i].icon);
					}

					instance.currentItem = instance.items[i];
					instance.currentItemImage = image;
					instance.currentItemContainer = itemContainer;
				};

				itemContainer.bind('click.iconicmenu', itemAction);
				Nokia.util.bindEnter(itemContainer, itemAction);

				itemContainer.hover(function() {
					Nokia.util.addClass( itemContainer[0], [Styles.iconicMenuSelected, Styles.uiStateHover].join(' ') );

					if (instance.items[i].hoverIcon && !instance.equalsItems(instance.currentItem, instance.items[i])) {
						image.attr('src', instance.items[i].hoverIcon);
					}
				}, function() {
					Nokia.util.removeClass( itemContainer[0], Styles.iconicMenuSelected );
					Nokia.util.removeClass( itemContainer[0], Styles.uiStateHover );

					if (instance.items[i].hoverIcon && !instance.equalsItems(instance.currentItem, instance.items[i])) {
						image.attr('src', instance.items[i].icon);
					} else {
						image.attr('src', instance.items[i].activeIcon);
					}
				});

				Nokia.dom.append(instance.wrapper, itemContainer);
			});

		},

		equalsItems: function(i1, i2) {
        	return i1.label == i2.label &&
        	i1.icon == i2.icon &&
        	i1.hoverIcon == i2.hoverIcon &&
        	i1.activeIcon == i2.activeIcon;
        },

		setItems: function(items) {
			var instance = this;

			instance.items = items;
		},

		select: function(item, event) {
			var instance = this,
				element = instance.element;

			item.select.apply(instance, [item, event]);
		},

		Styles: {
			iconicMenu: 'nokia-iconicmenu',
			iconicMenuItem: 'nokia-iconicmenu-item',
			iconicMenuImage: 'nokia-iconicmenu-image',
			iconicMenuLabel: 'nokia-iconicmenu-label',
			iconicMenuSelected: 'nokia-iconicmenu-item-selected',
			iconicMenuItemsWrapper: 'nokia-iconicmenu-item-wrapper',
			uiClearfix: 'ui-helper-clearfix',
			uiStateDefault: 'ui-state-default',
			uiStateHighlight: 'ui-state-active',
			uiStateHover: 'ui-state-hover',
			uiStateActive: 'ui-state-active'
		}
    });
})();